package com.kk.score.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kk.score.dao.UserDao;
import com.kk.score.entity.PositionEntity;
import com.kk.score.service.UserService;
import com.kk.score.utils.PageUtils;
import com.kk.score.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.kk.score.entity.PositionEntity;
import com.kk.score.service.PositionService;



/**
 * 
 *
 * @author KK
 * @email 409592048@qq.com
 * @date 2022-11-02 12:28:00
 */
@RestController
@RequestMapping("/position")
public class PositionController {
    @Autowired
    private PositionService positionService;
    @Autowired
    private UserDao userDao;

    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = positionService.queryPage(params);

        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @GetMapping("/listAll")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R userPageGetList(){
        return R.ok().put("data", positionService.list(new LambdaQueryWrapper<PositionEntity>().eq(PositionEntity::getStatus,1)));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R save(@RequestBody PositionEntity department){
        if (positionService.getOne(new LambdaQueryWrapper<PositionEntity>().eq(PositionEntity::getName, department.getName())) != null) {
            return R.error("此职位已存在！");
        } else {
            positionService.save(department);
            return R.ok();
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R update(@RequestBody PositionEntity department){
        PositionEntity one = positionService.getOne(new LambdaQueryWrapper<PositionEntity>().eq(PositionEntity::getName, department.getName()));
        if (!Objects.isNull(one) && !one.getUid().equals(department.getUid())) {
            return R.error("此职位已存在！");
        } else {
            positionService.updateById(department);
            return R.ok();
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R delete(@RequestBody String[] uids){
        if (uids == null || uids.length == 0){
            return R.error("请选择需要移到不可用的职位");
        }
        List<PositionEntity> positionEntities = new ArrayList<>();
        Arrays.asList(uids).forEach(i->{
            positionEntities.add(new PositionEntity(i,0));
        });
        positionService.updateBatchById(positionEntities);
        //将这些职位下的user移动到不可用
        return R.ok().put("data",userDao.deleteByPositionUids(uids,0));
    }

    /**
     * 彻底删除
     */
    @PostMapping("/cDelete")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R cDelete(@RequestBody String[] uids){
        if (uids == null || uids.length == 0){
            return R.error("请选择需要彻底删除的职位");
        }
        List<PositionEntity> positionEntities = new ArrayList<>();
        Arrays.asList(uids).forEach(i->{
            positionEntities.add(new PositionEntity(i,-1));
        });
        positionService.updateBatchById(positionEntities);
        //删除这些职位下的user
        return R.ok().put("data",userDao.deleteByPositionUids(uids,-1));
    }

    /**
     * 恢复
     */
    @PostMapping("/isT")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R isT(@RequestBody String[] uids) {
        if (uids == null || uids.length == 0){
            return R.error("请选择需要恢复的职位");
        }
        List<PositionEntity> positionEntities = new ArrayList<>();
        Arrays.asList(uids).forEach(i->{
            positionEntities.add(new PositionEntity(i,1));
        });
        positionService.updateBatchById(positionEntities);
        return R.ok();
    }

}
